﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection.Emit;
using System.Text;

namespace 优尼科加密程序DOS
{
    internal class Program
    {
        static List<JsonValue> json;

        static void Main(string[] args)
        {
            Initial();

            Console.WriteLine("Please Enter Password");
            string password = Console.ReadLine();
            Auth(password);
        }

        public static void Initial()
        {
            try
            {
                if (File.Exists("000"))
                {
                    Console.WriteLine(File.ReadAllText("000"));
                }
                else
                {
                    string contents = Guid.NewGuid().ToString();
                    Console.WriteLine(contents);
                    File.WriteAllText("000", contents);
                }


                if (File.Exists("ex.IOY") && Coop.AuthInitail())
                {
                    string text = File.ReadAllText("ex.IOY", Encoding.UTF8);
                    json = JsonConvert.DeserializeObject<List<JsonValue>>(text);
                    if (Coop.GetTime())
                    {
                        Start();
                    }
                }
            }
            catch (Exception ex)
            {
                File.WriteAllText("1.txt", ex.Message);
            }
        }

        private static void Auth(string pass)
        {
            try
            {
                if (Coop.GetTime())
                {
                    Start();
                }
                else
                {
                    JsonValue jsonValue = json.Find((x) => x.Password == pass);
                    if (jsonValue is null)
                    {
                        return;
                    }
                    Coop.InitialTime(jsonValue.ChooseTime);
                    if (Coop.GetTime())
                    {
                        Start();
                    }
                }
            }
            catch (Exception ex)
            {
                File.WriteAllText("2.txt", ex.Message);
            }

        }


        public static void Start()
        {
            try
            {
                Process.Start(json[0].AppName);
                Environment.Exit(0);
            }
            catch (Exception ex)
            {
                File.WriteAllText("3.txt", ex.Message);
            }
        }
    }
}
